package com.hlhj.carview.aty
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Iview.EquipmentView
import com.hlhj.carview.R
import com.hlhj.carview.adapter.EquipmentAdp
import com.hlhj.carview.bean.EquipmentListBean
import com.hlhj.carview.presenter.EquipmentPresenter
import kotlinx.android.synthetic.main.aty_carinfo.*
import kotlinx.android.synthetic.main.lo_pubhead.*
/**
 * Created by Never Fear   on 2018\12\21 0021.
Never More....
 */
class EquipmentAty :BaseAty<EquipmentView,EquipmentPresenter>(), EquipmentView {
    private var equipmentAdp:EquipmentAdp?=null
    private val datas=ArrayList<EquipmentListBean.InfoBean.DataBean>()
    private var page=1
    private var emptyView: View?=null
    override fun setEquipmentList(equipmentListBean: EquipmentListBean) {
        if (page==1){
            datas.clear()
        }
        datas.addAll(equipmentListBean.info.data)
        equipmentAdp?.notifyDataSetChanged()
    }
    override fun initView() {
        emptyView=LayoutInflater.from(this).inflate(R.layout.empty_equipment,null)
        tvTittle.text="设备列表"
        equipmentAdp= EquipmentAdp(datas)
        listView.adapter=equipmentAdp
        listView.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        equipmentAdp?.setEmptyView(emptyView)




    }
    override fun logicStart() {

        presenter?.getErquipmentList(page,this)

    }

    override fun setListener() {
        equipmentAdp?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, EquipmentDetailAty::class.java)
            intent.putExtra("id",datas[position].device_id)
            startActivityForResult(intent,1)
        }
        emptyView?.findViewById<View>(R.id.btGo)?.setOnClickListener {
            val intent = Intent(this, AskEquipmentAty::class.java)
            startActivityForResult(intent,1)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        if (requestCode==1){
            page=1
            presenter?.getErquipmentList(page,this)
        }
    }
    override fun bindView(): EquipmentView {
        return this
    }

    override fun createPresenter(): EquipmentPresenter {
        return EquipmentPresenter()
    }

    override fun getContentID(): Int {
        return R.layout.aty_equipment
    }
}